package de.gameduell.framework.gui.wrapper {
	import de.gameduell.framework.gui.behavior.BehaviorEvent;
	import de.gameduell.framework.gui.behavior.IBehavior;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;	

	/**
	 * this class wrapps the sprite class 
	 * inherit from it to give a class the 
	 * behavior of a sprite
	 * 
	 * ATTENTION:
	 * not every sprite functionality is implemented
	 * implement new sprite functionalities if nescessary
	 */
	public class SpriteWrapper extends DisplayObjectContainerWrapper {

		protected var _sprite:Sprite;
		protected var _behaviors:Array;

		public function SpriteWrapper(aSprite:Sprite){
			super(aSprite);
			_sprite = aSprite;
			_behaviors = new Array();
		}
		
		public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void{
			_sprite.startDrag(lockCenter,bounds);
		}
		
		public function stopDrag():void{
			_sprite.stopDrag();
		}
		
		public function set buttonMode(aMode:Boolean):void{
			_sprite.buttonMode = aMode;
		}
		
		public function get buttonMode():Boolean{
			return _sprite.buttonMode;
		}
		
		public function set useHandCursor(handCursor:Boolean):void{
			_sprite.useHandCursor = handCursor;
		}
		
		public function get useHandCursor():Boolean{
			return _sprite.useHandCursor;
		}
		
		public function set hitArea(aHitArea:Sprite):void{
			_sprite.hitArea = aHitArea;
		}
		
		public function get hitArea():Sprite{
			return _sprite.hitArea;
		}
		
		public function get dropTarget():DisplayObject{
			return _sprite.dropTarget;
		}

		/****************************************************************
		 *  					CUSTOM FUNCTIONS						*
		 ****************************************************************/
		 

		public function get sprite():Sprite{
			return _sprite;
		}
		
		public function addBehavior(behavior:IBehavior):void {
			behavior.init(this);
			_behaviors.push(behavior);
			var eventTypes:Array = behavior.getEventTypes();
			for(var i:* in eventTypes){
				behavior.addEventListener( eventTypes[i], onBehaviorEvent);
			}
		}
		
		private function onBehaviorEvent(event:BehaviorEvent):void {
			dispatchEvent(new BehaviorEvent(event.type,name));
		}

		public function removeBehavior(behavior:IBehavior):void{
			for(var i:* in _behaviors){
				if(_behaviors[i] == behavior){
					_behaviors.splice(i,1);
					behavior.active = false;
				}
			}
		}
		
		
	}
}
